package com.neouton.crm.payRecords.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neouton.common.util.AuthUtils;
import com.neouton.common.util.PageUtil;
import com.neouton.config.ResponseAPI;
import com.neouton.crm.cm.entity.SalesCostMigrateVO;
import com.neouton.crm.entity.SysUserEntity;
import com.neouton.crm.payRecords.entity.PaymentRecords;
import com.neouton.crm.payRecords.entity.PaymentRequest;
import com.neouton.crm.payRecords.service.PaymentRecordsService;
import com.neouton.crm.paymentPlan.entity.PaymentPlan;
import com.neouton.crm.paymentPlan.service.PaymentPlanService;
import com.neouton.crm.pm.entity.SalesProjectManageVO;
import com.neouton.crm.pm.service.ProjectMemberService;
import com.neouton.crm.pm.service.SalesProjectManageService;
import com.neouton.crm.receiptRecords.entity.ReceiptRecords;
import com.neouton.crm.sales.entity.SalesFileRelationVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;


/**
 * 付款记录
 *
 * @author
 * @date 2023-02-03 15:27:00
 * @Version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/payRecords")
public class PaymentRecordsController {

    @Resource
    private PaymentRecordsService paymentRecordsService;
    @Resource
    private ProjectMemberService projectMemberService;

    /**
     * 列表信息查询
     *
     * @param pageNum
     * @param pageSize
     * @param entity
     * @return
     */
    @PostMapping("/getPaymentRecords/{pageNum}/{pageSize}")
    public List<PaymentRecords> list(@PathVariable(value = "pageNum") int pageNum,
                                         @PathVariable(value = "pageSize") int pageSize,
                                         @RequestBody PaymentRecords entity) {

        List<PaymentRecords> result = paymentRecordsService.queryPage( entity);
        return result;
    }

    /**
     * 保存或修改付款记录
     * @param paymentRecords
     * @param token
     * @return
     */
    @PostMapping(value = "/savePaymentRecords")
    @ResponseBody
    public ResponseAPI savePaymentRecords(@RequestBody PaymentRecords paymentRecords,@RequestHeader("token") String token) {
        return ResponseAPI.successData(paymentRecordsService.savePaymentRecords(paymentRecords,token));
    }

    /**
     * 付款记录：数据修改
     */
    @RequestMapping("/updatePaymentRecords")
    @ResponseBody
    public ResponseAPI update(@RequestBody PaymentRecords entity,
                              @RequestHeader(value = "token", required = false) Integer token) {
            paymentRecordsService.updatePaymentRecordsById(entity);
        return ResponseAPI.successMes("修改模型数据成功");
    }
    /**
     * 查询付款记录
     * @param entity
     * @return
     */
    @PostMapping("/queryPayRecordedMoney/")
    public ResponseAPI queryPayRecordedMoney(@RequestBody PaymentRecords entity) {
        List<PaymentRecords> list =  paymentRecordsService.queryPayRecordedMoney(entity);
        return ResponseAPI.successData(list);
    }

    /**
     * 付款记录审批通过
     * @param entity
     * @param token
     * @return
     */
    @PostMapping("/approvedPayRecord")
    public ResponseAPI approvedPayRecord(@RequestBody PaymentRecords entity, @RequestHeader("token") String token) {
        return ResponseAPI.successData(paymentRecordsService.approvedPayRecord(entity,token,false));
    }
    /**
     * 付款记录审批驳回
     * @param entity
     * @param token
     * @return
     */
    @PostMapping("/rejectionPayRecord")
    public ResponseAPI rejectionPayRecord(@RequestBody PaymentRecords entity,@RequestHeader("token") String token) {
        return ResponseAPI.successData(paymentRecordsService.rejectionPayRecord(entity,token));
    }

    /**
     * 根据审批id获取付款记录历史数据
     * @param approvalId
     * @return
     */
    @PostMapping("/queryPayRecordHistory/{approvalId}")
    public ResponseAPI queryPayRecordHistory(@PathVariable(value = "approvalId") Integer approvalId) {
        return ResponseAPI.successData(paymentRecordsService.queryPayRecordHistory(approvalId));
    }

    /**
     * 查询付款记录
     *
     * @param paymentRecords
     * @return
     */
    @PostMapping("/queryPaymentRecordsListInfo/{pageNum}/{pageSize}")
    public ResponseAPI queryPaymentRecordsListInfo(@PathVariable(value = "pageNum") Integer pageNum,
                                                             @PathVariable(value = "pageSize") Integer pageSize,
                                                             @RequestBody PaymentRecords paymentRecords,
                                                             @RequestHeader("token") String token) {
        List<SysUserEntity> userList = projectMemberService.queryUserList(AuthUtils.getUserId(),"paymentRecords", token);
        IPage<PaymentRecords> page = new Page<>(pageNum, pageSize, true);
        PageUtil<PaymentRecords> list = paymentRecordsService.queryPaymentRecordsListInfo(page, paymentRecords, userList, token);
        return ResponseAPI.successData(list);
    }



    /**
     * 查询付款记录
     *
     * @param paymentRecords
     * @return
     */
    @PostMapping("/queryToDoPaymentRecordsListInfo/{pageNum}/{pageSize}")
    public ResponseAPI queryToDoPaymentRecordsListInfo(@PathVariable(value = "pageNum") Integer pageNum,
                                                   @PathVariable(value = "pageSize") Integer pageSize,
                                                   @RequestBody PaymentRecords paymentRecords,
                                                   @RequestHeader("token") String token) {
       // List<SysUserEntity> userList = projectMemberService.queryUserList(AuthUtils.getUserId(),"paymentRecords", token);
        IPage<PaymentRecords> page = new Page<>(pageNum, pageSize, true);
        PageUtil<PaymentRecords> list = paymentRecordsService.queryToDoPaymentRecordsListInfo(page, paymentRecords, token);
        return ResponseAPI.successData(list);
    }

    @PostMapping("/deletePayRecordsById/{payRecordsId}")
    public ResponseAPI deletePayedRequestById(@PathVariable(value = "payRecordsId") Integer payRecordsId) {
        Boolean flag= paymentRecordsService.deletePayRecordsById(payRecordsId);
        return ResponseAPI.successData(flag);
    }


    /**
     * Excel
     * @param response
     * @param entity
     * @throws Exception
     */
    @PostMapping("/exportInfo")
    public void exportInfo(HttpServletResponse response, @RequestBody PaymentRecords entity, @RequestHeader("token") String token) throws Exception{
        List<SysUserEntity> userList = projectMemberService.queryUserList(AuthUtils.getUserId(),"paymentRecords", token);
        List<PaymentRecords> list = paymentRecordsService.queryPaymentRecordsListInfo(entity, userList, token);
        paymentRecordsService.exportPaymentRecordsInfo(response,list,token);
    }
}
